<?php
MYTCONN();

	$AMPID = $_GET['AMPID'];

	echo '<table border="0" cellspacing="3" cellpadding="1">
	<tr>
    <td><div id="PBCPRC_Orders"></div></td>
    <td><div id="PBCPRC_Docs"></div></td>
  </tr></table>';

  $Qr_rs = $GLOBALS['MYT']->db_array("SELECT FraID AS O_ID, FraFechaIngreso AS COL1, FraEnviado AS COL4, CliNombre COL2, BnfNombre AS COL3, CONCAT(FraOrigen0,'-',FraReferenciaAg) AS COL5, sg_Descripcion AS COL6
  			FROM facturas IGNORE INDEX (IdxCli)
			INNER JOIN facturas_pbc ON FraID = frp_FraID
			INNER JOIN clientes ON clientes.CliNumero = Facturas.FraCliNumero
			INNER JOIN beneficiarios ON beneficiarios.BnfNumero = Facturas.FraBnfNumero
			INNER JOIN hd_statusfacturas ON sg_ID = FraStatus
			WHERE frp_PbcID = {$AMPID}");

  $Qr_rsDocs = $GLOBALS['MYT']->db_array("SELECT
  dtd_ID AS O_ID, dtd_Fecha AS COL1, FILE AS COL4, tdc_Tipo AS COL2, dtd_Numero AS COL5, DC_OFFICE AS COL6
  			FROM  hd_documentos
			INNER JOIN hd_documentostipos ON dtd_Tipo = tdc_Id
			WHERE dtd_ID IN (SELECT add_DocID FROM hd_amlprdoc WHERE add_prID = {$AMPID})
	UNION
	SELECT
  dnl_ID AS O_ID, dnl_FechaEntrega AS COL1, dnl_FILE AS COL4, dnc_Doc AS COL2, dnl_Observ AS COL5, dnl_OFFICE AS COL6
  			FROM hd_docdocumentos
			INNER JOIN hd_doctipos ON dnc_ID = dnl_TipoDoc
			WHERE dnl_ID IN (SELECT add_DocDocID FROM hd_amlprdoc WHERE add_prID = {$AMPID})");
$CARD = $Qr_rs[0];
$CARDDOCS = $Qr_rsDocs[0];
$SUM = 0; ?>
<script charset="utf-8" type="text/javascript">
disableSelection(document.getElementById("PBCPRC_Orders"));
disableSelection(document.getElementById("PBCPRC_Docs"));
Ext.onReady(function(){

    var myData = [
	<?php
	for ($i=1;$i<=$CARD;$i++) {
	$SUM = $SUM+$Qr_rs[$i]['COL4'];
	echo '['.$i.','.$Qr_rs[$i]['O_ID'].',\''.$Qr_rs[$i]['COL1'].'\',\''.$Qr_rs[$i]['COL2'].'\',\''.$Qr_rs[$i]['COL3'].'\',\''.number_format($Qr_rs[$i]['COL4'],2,',','').'\',\''.$Qr_rs[$i]['COL5'].'\',\''.$Qr_rs[$i]['COL6'].'\']';
		if ($i<$CARD) {
			echo ',';
		}
    } ?>
    ];

    var ds = new Ext.data.Store({
        reader: new Ext.data.ArrayReader({}, [
               {name: 'id'},
			   {name: 'O_ID', type: 'int'},
               {name: 'COL1', type: 'string'},
               {name: 'COL2', type: 'string'},
               {name: 'COL3', type: 'string'},
			   {name: 'COL4', type: 'string'},
			   {name: 'COL5', type: 'string'},
			   {name: 'COL6', type: 'string'}
          ])
    });
    ds.loadData(myData);
    var colModel = new Ext.grid.ColumnModel([
    {header: "N", width: 40, sortable: true, locked:true, dataIndex: 'id'},
		{header: "Operacao", width: 60, sortable: true, locked:true, dataIndex: 'O_ID'},
		{header: "Remessa", width: 70, sortable: true, locked:true, dataIndex: 'COL5'},
        {header: "Data", width: 120, sortable: true, locked:true, dataIndex: 'COL1'},
        {header: "Remetente", width: 200, sortable: true, dataIndex: 'COL2'},
        {header: "Destinatario", width: 200, sortable: true, locked:true,dataIndex: 'COL3'},
        {header: "Montante", width: 100, sortable: true, align:'right', locked:true,dataIndex: 'COL4'},
        {header: "Estado", width: 130, sortable: true, align:'right', locked:true,dataIndex: 'COL6'}
    ]);

	Bars = new Ext.Toolbar({

					store: this.store,
					height: 25,
					align: 'right',
					items : [ {
						text: "# <?php echo $CARD;?> | EUR: <?php echo number_format($SUM,2);?>"
				},{
						type: 'button',
						text: 'Remover Items Seleccionados',
				handler:function(){
						var ss = '';
						var m = selmode.getSelections();
						for(var i = 0, len = m.length; i < len; i++){
						ss += m[i].get("O_ID") + ",";}
						document.MQuery.cpl_Selected.value = ss;
						document.MQuery.action='_response/call.php?get=Compliance/cpl_rmvassc&AMPID=<?php echo $_GET['AMPID'];?>';
						subform('MQuery','row<?php echo $_GET['AMPID'];?>');
					}
				}]
				});

   var selmode = new Ext.grid.RowSelectionModel({});
   var Console = new Ext.grid.GridPanel({
   	title: 'Remessas Associadas',
            columnWidth: 0.6,
	            height: 200,
				width: 450,
	            xtype: 'grid',
	            ds: ds,
	            cm: colModel,
	            sm: selmode,
				listeners: {
	 				rowdblclick: function(ds, row) {
					var record = ds.getStore().getAt(row);
  					var value = record.get('O_ID');
					ShowProp(value);
					 }
				},
	            border: true,
				bbar: Bars,
		renderTo: 'PBCPRC_Orders'
    });

    var myDataDoc = [
	<?php
	for ($i=1;$i<=$CARDDOCS;$i++) {
	echo '['.$i.','.$Qr_rsDocs[$i]['O_ID'].',\''.$Qr_rsDocs[$i]['COL1'].'\',\''.$Qr_rsDocs[$i]['COL2'].'\',\''.$Qr_rsDocs[$i]['COL4'].'\',\''.$Qr_rsDocs[$i]['COL5'].'\',\''.$Qr_rsDocs[$i]['COL6'].'\']';
		if ($i<$CARDDOCS) {
			echo ',';
		}
    } ?>
    ];

    var dsDoc = new Ext.data.Store({
        reader: new Ext.data.ArrayReader({}, [
               {name: 'id'},
			   {name: 'O_ID', type: 'int'},
               {name: 'COL1', type: 'string'},
               {name: 'COL2', type: 'string'},
			   {name: 'COL4', type: 'string'},
			   {name: 'COL5', type: 'string'},
			   {name: 'COL6', type: 'string'}
          ])
    });
    dsDoc.loadData(myDataDoc);
    var colModelDoc = new Ext.grid.ColumnModel([
    {header: "N", width: 40, sortable: true, locked:true, dataIndex: 'id'},
		{header: "Documento", width: 60, sortable: true, locked:true, dataIndex: 'O_ID'},
		{header: "Numero", width: 70, sortable: true, locked:true, dataIndex: 'COL5'},
        {header: "Data", width: 120, sortable: true, locked:true, dataIndex: 'COL1'},
        {header: "Tipo", width: 200, sortable: true, dataIndex: 'COL2'},
        {header: "Arquivo", width: 100, sortable: true, align:'right', locked:true,dataIndex: 'COL4'},
        {header: "Loja de Arquivo", width: 130, sortable: true, align:'right', locked:true,dataIndex: 'COL6'}
    ]);

	BarsDoc = new Ext.Toolbar({

					store: this.store,
					height: 25,
					align: 'right',
					items : [ {
						text: "# <?php echo $CARD;?>"
				} ]
				});

   var selmodeDoc = new Ext.grid.RowSelectionModel({});
   var ConsoleDoc = new Ext.grid.GridPanel({
   	title: 'Documentos Associados',
	            height: 200,
				width: 350,
            columnWidth: 0.6,
	            xtype: 'grid',
	            ds: dsDoc,
	            cm: colModelDoc,
	            sm: selmodeDoc,
				listeners: {
	 				rowdblclick: function(ds, row) {
					var record = dsDoc.getStore().getAt(row);
  					var value = record.get('O_ID');
					ShowProp(value);
					 }
				},
	            border: true,
				bbar: Bars,
		renderTo: 'PBCPRC_Docs'
    });

});
</script>